﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit;
using MyWebPagesStarterKit.Controls;
using System.Data;


public partial class SectionControls_CustomData : SectionControlBaseClass
{
    private CustomData _section;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        UpdateViews();
    }

    private void UpdateViews()
    {
        if (ViewMode == ViewMode.Edit)
        {
            multiview.SetActiveView(editView);

            DataTable dt = CustomHtml.GetInstance().Entries;

            drpHtmlSelect.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                ListItem item = new ListItem(Convert.ToString(row["Title"]),Convert.ToString(row["Guid"]));
                if (Convert.ToString(row["Guid"]) == _section.SectionID)
                    item.Selected = true;

                drpHtmlSelect.Items.Add(item);
            }
            drpHtmlSelect.Items.Insert(0,new ListItem("---------------",""));

        }
        else
        {
            multiview.SetActiveView(readonlyView);
            litHtml.Text = _section.GetHtml(_section.SectionID);
        }
    }

    protected void Save(object sender, EventArgs e)
    {
        if (drpHtmlSelect.SelectedValue != "---------------")
        {
            _section.SectionID = drpHtmlSelect.SelectedValue;
            _section.SaveData();
        }
    }

    public override bool HasAdminView
    {
        get { return true; }
    }

    public override string InfoUrl
    {
        get { return ""; }
    }

    public override ISection Section
    {
        set
        {
            if (value is CustomData)
                _section = (CustomData)value;
            else
                throw new ArgumentException("Section must be of type CustomData");
        }
        get { return _section; }
    }
}
